//
//  CloseViewController.m
//  zuixin
//
//  Created by QJY on 14-8-18.
//  Copyright (c) 2014年 dingshenwangluokeji. All rights reserved.
//

#import "CloseViewController.h"
#import "Header.h"
#import "NetAccess.h"
#import "MBProgressHUD.h"
#import "AFNetworking.h"
#import "Base64Encry.h"
@interface CloseViewController ()
@end

@implementation CloseViewController
{ MBProgressHUD *pro;
    
    NetAccess *_net;
    NSString *_word;
    NSString *_name;
    NSString*_phoneName;
    NSUserDefaults *users;
    NSMutableData *_data;
    UILabel *_label;
    UILabel *_label1;
    UILabel *_label2;
    float _string;
    NSArray*arry;
    NSArray*arry1;
    UITextField *textf;
    UITextField *textf1;
    UITextField *textf2;

    NSDictionary*dicts;
    UILabel* label6;
    
    
    
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
//-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
//{
//    _data =[[NSMutableData alloc]init];
//}
//-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
//{
//    [_data appendData:data];
//
//}
//-(void)connectionDidFinishLoading:(NSURLConnection *)connection
//{
//    NSDictionary *rootDict =[NSJSONSerialization JSONObjectWithData:_data options:NSJSONReadingMutableContainers error:nil];
//    NSArray *arrays =[rootDict objectForKey:@"infoCustomers"];
//    if(arrays.count==0)
//    {[pro removeFromSuperview];
//    }else{
//        NSDictionary *ed =[arrays objectAtIndex:0];
//        NSString *gu =[ed objectForKey:@"name"];
//        _string =[[ed objectForKey:@"money"] integerValue];
//        [pro removeFromSuperview];
//        _label2.text =[NSString stringWithFormat:@"话费余额:  %f元",_string];
//        _label1.text =[NSString stringWithFormat:@"手机号:  %@",gu];
//    }
//}
-(void)match
{
    //pro = [[MBProgressHUD alloc] initWithView:self.view];
    
    pro=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    pro.labelText = @"请稍后..";
//    pro.mode = MBProgressHUDModeText;
//    [self.view addSubview:pro];
   
    //[pro showAnimated:YES whileExecutingBlock:^{
        if(![_net haveNet])
        {
            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"无网络连接" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
            
        }
        else{
            [self getStarte];
//            [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(getStarte:) object:nil];
//            [self performSelector:@selector(getStarte:) withObject:nil afterDelay:1.0f];
        }
 

//    } completionBlock:^{
//        [self setView];
//        [self setView2];
//        
//    }];
}
//-(void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    [self controllersd];
//    
//    }
-(void)getStarte
{
//        NSString *str=[NSString stringWithFormat:@"http://oemapi2.leehuu.cn/api/vos/GetUserInfo?username=%@&Password=%@&parent=62634",_name,_word];
//      NSLog(@"22%@ %@",_word,_name);
//    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//   // NSURLRequest *request = [NSURLRequest requestWithURL:url];
//    NSMutableURLRequest*request=[[NSMutableURLRequest alloc]initWithURL:url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0f];
//    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
//    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSString *html = operation.responseString;
//        NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
//        if (data==nil) {
//            [pro removeFromSuperview];
//            UIAlertView*alertV=[[UIAlertView alloc]initWithTitle:@"提醒" message:@"服务暂不可用，请稍后查询" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
//            [alertV show];
//            return ;
//        }else
//        {
    NSString*urlStr=@"http://oemapi2.leehuu.cn/YXBp/dm9zb2Vt/R2V0VXNlckluZm8PQ";
    NSString * parameterStr=[NSString stringWithFormat:@"dXNlck5hbWUPQ=%@&password=%@&parent=2645421",[Base64Encry switchBase64String:_name],_word];
    NSLog(@"登录%@",[NSString stringWithFormat:@"%@  %@",urlStr,parameterStr]);
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    id dict=[Base64Encry switchUrlString:urlStr switchParameterString:parameterStr];
         if ([dict count]) {
             dispatch_async(dispatch_get_main_queue(), ^{NSString*recodeString=[[dict objectForKey:@"retCode"]stringValue];
                 if (![recodeString isEqualToString:@"0"]) {
                     [pro removeFromSuperview];
                     NSString*exceptionString=[dict objectForKey:@"exception"];
                     UIAlertView*alet=[[UIAlertView alloc ]initWithTitle:@"提醒" message:exceptionString delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                     [alet show];
                 }
                 else
                 {
                     dicts=[dict objectForKey:@"accountInfo"];
                     NSLog(@"1111111d%@",dicts);
                     //double ss = [[dicts objectForKey:@"balance"] doubleValue];
                     [pro removeFromSuperview];
                     
                     [self setView];
                     [self setView2];
                     
                     
                 }

             });
         }
     });
    
                    }
//    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        [pro removeFromSuperview];
//        UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@"提醒" message:@"网络请求错误，请稍后再试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
//        [alert show];
//        return ;
//    }];
//    
//    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//    [queue addOperation:operation];
//}
//    if (dicts==nil) {
//        UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@"提醒" message:@"服务器有异常，请重新链接" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
//        [alert show];
//    }
//    else
//    {
//}
-(void)setView
{
    UILabel*label1=[[UILabel alloc]initWithFrame:CGRectMake(0, 20-1, 160, 44)];
    label1.text=@"   手机";
    label1.backgroundColor=[UIColor whiteColor];
    //[self.view addSubview:label1];
    UILabel*label2=[[UILabel alloc]initWithFrame:CGRectMake(160, 20-1, 160, 44)];
    label2.text=_phoneName;
    label2.textAlignment=2;
    label2.backgroundColor=[UIColor whiteColor];
    //[self.view addSubview:label2];
    UIImageView*imV1=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"gexian.png"]];
    imV1.frame=CGRectMake(0, 64, 320, 1);
    //[self.view addSubview:imV1];
    UILabel*label3=[[UILabel alloc]initWithFrame:CGRectMake(0, 64+1, 160, 44)];
    label3.text=@"   手机号:";
    label3.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:label3];
    UILabel*label4=[[UILabel alloc]initWithFrame:CGRectMake(160, 64+1, 160, 44)];
    label4.text=[NSString stringWithFormat:@"%@",_name];
    label4.textAlignment=2;
    label4.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:label4];
    
    
    UILabel*label5=[[UILabel alloc]initWithFrame:CGRectMake(0, 128-1, 160, 44)];
    label5.text=@"   可用余额";
    label5.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:label5];
    UIImageView*imV2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"gexian.png"]];
    imV2.frame=CGRectMake(0, 128+44, 320, 1);
    
    [self.view addSubview:imV2];
    label6=[[UILabel alloc]initWithFrame:CGRectMake(160, 128-1, 160, 44)];
    label6.textAlignment=2;
    double ss = [[dicts objectForKey:@"balance"] doubleValue];
    
    label6.text=[NSString stringWithFormat:@"%@元",[NSString stringWithFormat:@"%.2f",ss]];
    
    label6.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:label6];
    
}
//取到小数点后两位
//-(NSString*)getMoneyNumber:(NSString*)string
//{
//    NSLog(@"2233%@",string);
//    if ([string rangeOfString:@","].location!=NSNotFound)
//    {
//        NSArray*arry1=[string componentsSeparatedByString:@","];
//        NSLog(@"3333%@",arry1);
//        NSString*st1=arry1[1];
//        if (st1.length > 2)
//          {
//            NSString*st2=[st1 substringWithRange:NSMakeRange(0, 2)];
//            NSLog(@"hahhh%@",st2);
//            NSString*st3=[NSString stringWithFormat:@"%@%@",arry1[0],arry1[1]];
//            return st3;
//            }
//        else
//        {
//
//            return string ;
//        }
//    }
//        else
//        {
//            return string;
//        }
//
//
//}
-(void)setView2
{
    UIImageView*imV3=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"gexian.png"]];
    imV3.frame=CGRectMake(0, 128+44*2, 320, 1);
    //[self.view addSubview:imV3];
    UILabel*label5=[[UILabel alloc]initWithFrame:CGRectMake(0, 128+44+1, 160, 44-2)];
    label5.text=@"   截止日期";
    label5.backgroundColor=[UIColor whiteColor];
    //[self.view addSubview:label5];
    
    UILabel*label61=[[UILabel alloc]initWithFrame:CGRectMake(160, 128+44+1, 160, 44-2)];
    label61.textAlignment=2;
    NSLog(@"1111%@",[dicts objectForKey:@"isMonth"]);
   
        label61.text=@"2015年5月31日";
   
    label61.backgroundColor=[UIColor whiteColor];
    //[self.view addSubview:label61];
    }
-(void)tap:(UIButton*)sender
{
    
    
    switch (sender.tag) {
        case 10:
            textf.text =[[[[arry[0] objectForKey:@"money"] stringValue] substringToIndex:1] stringByAppendingString:@"元"];
            
            break;
        case 11:
            textf1.text=[arry[0] objectForKey:@"name"] ;
            break;
            
        case 12:
            textf2.text=[[[arry[0] objectForKey:@"todayConsumption"] stringValue] stringByAppendingString:@"元"] ;
            break;
            
            
        default:
            break;
    }
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"基本信息";
    if (IOS7) {
        self.edgesForExtendedLayout=NO;
    }
	// Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor colorWithWhite:0.9f alpha:1];
    _net = [NetAccess sharedNetAccess];
   [self controllersd];
    [self addBack];
    
    [self match];
    
}
-(void)addBack
{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 28, 28);
    
    [backBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    
    // Do any additional setup after loading the view.
    
}
-(void)doBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//    UIImageView *imageView1 =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
//    imageView1.frame =CGRectMake(0, 0,320, 120);
//    [self.view addSubview:imageView1];
//    UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(0, 10, 320, 40)];
//    label.text=@"充满阳光，让我们携手共进";
//    label.textAlignment=1;
//    label.textColor=[UIColor whiteColor];
//    label.font=[UIFont systemFontOfSize:17];
//    [self.view addSubview:label];
//    UILabel*label1=[[UILabel alloc]initWithFrame:CGRectMake(0, 50, 320, 40)];
//    label1.text=@"忠  和  信";
//    label1.textAlignment=1;
//    label1.textColor=[UIColor whiteColor];
//    label1.font=[UIFont systemFontOfSize:24];
//
//    [self.view addSubview:label1 ];
//    UILabel*label2=[[UILabel alloc]initWithFrame:CGRectMake(0, 70, 320, 40)];
//    label2.text=@"-－中和信";
//    label2.textAlignment=2;
//    label2.textColor=[UIColor whiteColor];
//    label2.font=[UIFont systemFontOfSize:16];
//
//    [self.view addSubview:label2 ];
//    for (int i=0; i<5; i++) {
//
//       UIImageView *imageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"LOGO.png"]];
//        imageView.frame =CGRectMake(250+15*i, 100 ,10, 20);
//       [imageView1 addSubview:imageView];
//        UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0,imageView1.frame.size.height , 320, DeviceHeight-64-44-imageView1.frame.size.height)];
//        view.backgroundColor=[UIColor blueColor];
//        [self.view addSubview:view];
//        label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 20)];
//        label.font=[UIFont systemFontOfSize:15];
//        la
//        [view addSubview:label];
//    }

//
//    _label =[[UILabel alloc]init];
//    _label.frame=CGRectMake(60, imageView.frame.origin.y+80, 260, 35);
//    _label.backgroundColor =[UIColor whiteColor];
//    [self.view addSubview:_label];
//    _label1 =[[UILabel alloc]init];
//    _label1.frame=CGRectMake(60, _label.frame.origin.y+40, 260, 35);
//    _label1.backgroundColor =[UIColor whiteColor];
//    [self.view addSubview:_label1];
//    _label2 =[[UILabel alloc]init];
//    _label2.frame=CGRectMake(60, _label1.frame.origin.y+40, 260, 35);
//    _label2.backgroundColor =[UIColor whiteColor];
//    [self.view addSubview:_label2];
//秒数转换成日期
-(NSString*)switchTime:(NSString*)timeStr
{
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:[timeStr doubleValue]/1000];
    // NSLog(@"dddd:%@",d);
    //2011-01-18 13:00:00 +0000
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
    [formatter1 setDateFormat:@"yyyy-MM-dd"];
    NSString *showtimeNew = [formatter1 stringFromDate:d];
    // NSLog(@"showtimeNew:%@",showtimeNew);        // NSArray *lists =[d]
    return showtimeNew;
}

-(void)controllersd
{
    users =[NSUserDefaults standardUserDefaults];
    NSArray *array =[users objectForKey:@"newusers1"];
    NSDictionary *dict =[array objectAtIndex:0];
    
    _name =[dict objectForKey:@"username"];
    _word=[dict objectForKey:@"userword"];
    //    _phoneName=[dict objectForKey:@"usercellphone" ];
    //dicts=[users objectForKey:@"Dic"];
    NSLog(@"qq%@%@",_name,_word);
}
-(void)btnClick
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
